package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.MoneysTotalBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 我的钱包 余额界面
 */
public class MyMoneyBagActivity extends BaseActivity implements View.OnClickListener {
    private ImageView text_back;//返回
    private TextView text_title;//标题
    private RelativeLayout rl_iv_back;
    private TextView tv_money;
    private RelativeLayout rl_chognzhi, rl_tixian, rl_jifen;
    private MoneysTotalBean moneysTotalBean = new MoneysTotalBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_money_bag);
        initView();
    }

    private void initView() {
        tv_money = (TextView) findViewById(R.id.tv_money);
        rl_chognzhi = (RelativeLayout) findViewById(R.id.ll_chongzhi);
        rl_tixian = (RelativeLayout) findViewById(R.id.ll_tixian);
        rl_chognzhi.setOnClickListener(this);
        rl_tixian.setOnClickListener(this);
        text_back = (ImageView) findViewById(R.id.text_back);
        text_title = (TextView) findViewById(R.id.text_title);
        //top_right_tv= (TextView) findViewById(R.id.top_right_tv);
        text_back.setOnClickListener(this);
        text_title.setText("我的钱包");
        getServerData();
    }

    /*请求服务器数据*/
    private void getServerData() {
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("user_id", MyApp.getInstance().getUser_id());
            }
        };
        NetHelper.post(APIServer.MYBALANCE, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                /*请求得到的数据*/
                JSONObject object = null;
                try {
                    object = new JSONObject(s);
                    if (object.getString("code").equals("200")) {
                        String money = object.getJSONObject("data").getString("user_money");
                        tv_money.setText(money);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //添加点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_back: //返回
                finish();
                break;
            case R.id.ll_chongzhi://充值
                Intent intent = new Intent(this, AccountRechargeActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_tixian://提现
                Intent intent1 = new Intent(this, AccountCashActivity.class);
                intent1.putExtra("yu_e", tv_money.getText());
                startActivity(intent1);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getServerData(); //再次请求设置数据
    }
}
